import { createSlice } from "@reduxjs/toolkit";
const tableSlice = createSlice({
    name:'table',
    initialState:{
        tablelist:[]
    },
    reducers:{
        setlist(state,{payload,type}){
            state.tablelist = payload
        },
        del(state,{payload,type}){
            state.tablelist.forEach((item,index)=>{
                if(item.key === payload){
                    state.tablelist.splice(index,1)
                }
            })
        },
        add_table(state,{payload,type}){
            state.tablelist.unshift(payload)
        },
        edit_table(state,{payload,type}){
            state.tablelist.forEach((item,index)=>{
                if(item.key===payload.key){
                    item.name = payload.name
                    item.age = payload.age
                    item.sex = payload.sex
                    item.price = payload.price
                    item.pro = payload.pro
                }
            })
        },
        alldel_table(state,{payload,type}){
            payload.forEach((item,index)=>{
                state.tablelist.forEach((v,i)=>{
                    if(v.key===item){
                        state.tablelist.splice(i,1)
                    }
                })
            })
        }   
    }
})
export const {setlist,del, add_table,edit_table,alldel_table} = tableSlice.actions
export default tableSlice.reducer